"""
Created on 24-Sep-2009

@author: dexen
"""

import os
import sys

from PyQt4.QtGui import *            
from PyQt4 import QtCore

import frontend

#===============================================================================
# Schema Dialog
#===============================================================================
class SchemaDialog(QDialog):
    def __init__(self, parent=None):
        super(SchemaDialog, self).__init__(parent)
        self.setGeometry(300,300,400, 400)
        self.setWindowTitle("Create a New Schema")
        layout = QGridLayout()
        layout.addWidget(QLabel("Schema Name:"), 0, 0)
        self.line_edit = QLineEdit()
        layout.addWidget(self.line_edit, 0, 1)
        layout.addWidget(QLabel("Schema path:"), 1, 0)
        browse_button = QPushButton("Browse")
        browse_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        button_box = QDialogButtonBox(QDialogButtonBox.Ok | 
                                      QDialogButtonBox.Cancel)
        self.connect(button_box, QtCore.SIGNAL("accepted()"), self.on_accept)
        self.connect(button_box, QtCore.SIGNAL("rejected()"), self, 
                     QtCore.SLOT("reject()"))
        
        layout.addWidget(button_box, 2, 1)
        self.connect(browse_button, QtCore.SIGNAL("clicked()"), self.on_browse)
        layout.addWidget(browse_button, 1, 1)
        self.setLayout(layout)
        self.adjustSize()
        self.schemapath = ""
    
    
    def on_accept(self):
        self.close()
        self.schemaname = self.line_edit.text()
        print self.schemapath, self.schemaname
        #os.path.join(str(self.schemapath), str(self.schemaname))
        frontend.create_new_schema(str(self.schemaname), str(self.schemapath))
        
    def on_browse(self):
        self.schemapath = QFileDialog.getExistingDirectory(self, 
                "Choose Schema Folder", os.getcwd(), QFileDialog.ShowDirsOnly)
        print self.schemapath               

    def closeEvent(self, event):
        assert isinstance(event, QCloseEvent)
        print "It is closing"
        event.accept()

#===============================================================================
# Schema Panel
#===============================================================================
class TreeWidget(QTreeWidget):
    def __init__(self):
        super(TreeWidget, self).__init__()
        self.setColumnCount(2)
        self.top_item = QTreeWidgetItem()
        self.top_item.setText(0, "Schemas")
        button = QPushButton("Deneme")
        button.setAutoFillBackground(True)
        self.setItemWidget(self.top_item, 1, QPushButton("dd", None))

        self.insertTopLevelItem(0, self.top_item)
        self.connect(self, QtCore.SIGNAL("itemClicked(QTreeWidgetItem *, int)"),
                     self.on_click)
                       
    def on_click(self, item, col):
        print "I am clicked", item, col


#===============================================================================
# Main Window
#===============================================================================
class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setWindowTitle("DEXEN Framework")
        self.setGeometry(300, 300, 320, 320)
        self.set_layouts()
        self.create_menu()
        self.create_schema_panel()
    
    def set_layouts(self):
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        self.layout = QGridLayout(central_widget)
    
    def create_schema_panel(self):
        self.tree = TreeWidget()
        self.layout.addWidget(self.tree, 0, 0)
    
    def create_menu(self): 
        self.menubar = QMenuBar(self)
        self.setMenuBar(self.menubar)
        menu = self.menubar.addMenu("Schema")
        assert isinstance(menu, QMenu)
        newSchemaAct = QAction("New", qApp)
        newSchemaAct.setShortcut(QKeySequence(QtCore.Qt.CTRL + QtCore.Qt.Key_N))
        self.connect(newSchemaAct, QtCore.SIGNAL("triggered()"), 
                     self.create_new_schema)
        menu.addAction(newSchemaAct)    
    
    def create_new_schema(self):
        print "Create new schema"
        dialog = SchemaDialog(self)

#===============================================================================
# Main
#===============================================================================
def main():
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    #frontend.initialize()
    sys.exit(app.exec_())
    
if __name__ == "__main__":
    main()